package org.example.day02;

import java.util.Random;

/**
 * 第一章节1.10.2：优先级具有规则性
 */
public class MyThread7 extends Thread{
    @Override
    public void run() {
        long beginTime = System.currentTimeMillis();
        long addResult = 0;
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 50000; j++) {
                Random random = new Random();
                random.nextInt();
                addResult = addResult + i;
            }
        }
        long endTime = System.currentTimeMillis();
        System.out.println("★ ★ ★ ★ use time = " + (endTime-beginTime));
    }

    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            MyThread7 m7 = new MyThread7();
            m7.setPriority(1);
            m7.start();
            MyThread8 m8 = new MyThread8();
            m8.setPriority(10);
            m8.start();
        }
    }
}

class MyThread8 extends Thread{
    @Override
    public void run() {
        long beginTime = System.currentTimeMillis();
        long addResult = 0;
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 50000; j++) {
                Random random = new Random();
                random.nextInt();
                addResult = addResult + i;
            }
        }
        long endTime = System.currentTimeMillis();
        System.out.println("☆ ☆ ☆ ☆ use time = " + (endTime-beginTime));
    }
}
//m7优先级为10  m8优先级为1
//★ ★ ★ ★ use time = 186
//★ ★ ★ ★ use time = 187
//★ ★ ★ ★ use time = 192
//★ ★ ★ ★ use time = 228
//★ ★ ★ ★ use time = 228
//☆ ☆ ☆ ☆ use time = 267
//☆ ☆ ☆ ☆ use time = 262
//☆ ☆ ☆ ☆ use time = 194
//☆ ☆ ☆ ☆ use time = 167
//☆ ☆ ☆ ☆ use time = 287

//m7优先级为1  m8优先级为10
//☆ ☆ ☆ ☆ use time = 164
//☆ ☆ ☆ ☆ use time = 189
//☆ ☆ ☆ ☆ use time = 193
//☆ ☆ ☆ ☆ use time = 193
//☆ ☆ ☆ ☆ use time = 206
//★ ★ ★ ★ use time = 122
//★ ★ ★ ★ use time = 148
//★ ★ ★ ★ use time = 109
//★ ★ ★ ★ use time = 171
//★ ★ ★ ★ use time = 119